package cn.zwt.wolf.service.rule;

import cn.zwt.wolf.domain.arrange.Arrange;
import cn.zwt.wolf.domain.content.Content;
import cn.zwt.wolf.domain.rule.Rule;
import cn.zwt.wolf.repository.rule.RuleRepository;
import cn.zwt.wolf.service.arrange.ArrangeService;
import cn.zwt.wolf.util.JPQLSearchException;
import cn.zwt.wolf.util.SearchUtil;
import org.apache.poi.util.StringUtil;
import org.codehaus.plexus.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.NoResultException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangwt on 16-3-13.
 */
@Service
public class RuleService {
    @Autowired
    private RuleRepository ruleRepository;
    @Autowired
    private SearchUtil searchUtil;
    @Autowired
    private ArrangeService arrangeService;
    private Logger logger = LoggerFactory.getLogger(RuleService.class);

    public Rule findRuleById(int id){
        return  ruleRepository.findOne(id);
    }

    public Rule findRuleByCode(String code){
        Rule rule =new Rule();
        if (code == null)
            return rule;
        String hql = "from Rule where status = 1 and code=:code";
        Map<String,Object> params= new HashMap<String, Object>();
        params.put("code",code);
        try {
            rule = (Rule) searchUtil.getSingleResult(hql, params);
        } catch (JPQLSearchException e) {
            logger.error("findRuleByCode error :" + e.getMessage());
        }catch (NoResultException e){
            logger.error("findRuleByCode error :" + e.getMessage());
        }
        return rule;
    }

}
